<?php
/**
 * > モンスターデータ
 *
 * LICENSE: Miracle Maple by Mirage is licensed
 * under a Creative Commons 表示-非営利-継承 2.1 日本 License.
 *
 * @category   monster
 * @package    maple-story.jp
 * @version    8.0.0
 * @link       http://maple-story.jp/monster/
 * @since      File available since Release 8.0.0
 * @copyright  Copyright 2011 Miracle Maple
 * @license    http://creativecommons.org/licenses/by-nc-sa/2.1/jp/     Creative Commons BY-NC-SA
 */
class MonsterController extends Zend_Controller_Action
{
	protected $_monster;
	
	public function init()
	{
		$this->view->headLink()->appendStylesheet('/css/jquery.ui.css')
		                       ->appendStylesheet('/css/jquery.ui.custom.css')
							   ->appendStylesheet('/css/monster.css');
		$this->view->headScript()->appendFile('/js/jquery.ui.js')
		                         ->appendFile('/js/monster.js');
		$this->_monster = new Model_TrnMonster();
	}
	
    /**
     * トップ
     */
    public function indexAction()
    {
		// パンくず
        $topicPath = array();
        $topicPath[] = array('name' => 'モンスター');
		$this->view->topicPath = $topicPath;
		$this->view->headTitle('モンスター', 'PREPEND');
   }
	
	public function searchAction()
	{
		$rowsMonster = $this->_monster->search($this->getRequest()->getParams());
		$this->view->monsters = $rowsMonster;
		
		// 検索フォームに表示
		$this->view->monsterCount = count($rowsMonster);
		
		$articleTitle = 'モンスター';
		if ($this->_getParam('lv')) {
			$articleTitle = 'Lv ' . $this->_getParam('lv') . ' のモンスター';
			$levelRange = explode('-', $this->_getParam('lv'));
			$this->view->minLevel = $levelRange[0];
			$this->view->maxLevel = $levelRange[1];			
		} else if ($this->_getParam('area')) {
			$this->view->area = $this->_getParam('area');
			$articleTitle = $this->_getParam('area') . ' に出現するモンスター';
		}
		
		$this->view->articleTitle = $articleTitle;
		$this->view->headTitle($articleTitle, 'PREPEND');
		$this->view->headScript()->appendFile('/js/jquery.quicksand.js');
		
		// パンくず
		$topicPath = array();
        $topicPath[] = array('url' => '/monster/', 'name' => 'モンスター');
		$topicPath[] = array('name' => $articleTitle);
		$this->view->topicPath = $topicPath;
	}
    
    public function dataAction()
    {
        $rowMonster = $this->_monster->find($this->_getParam('id'));
		
		
		if (!$rowMonster instanceof Model_TrnMonster) {
			throw new Zend_Controller_Action_Exception('Invalid Data Model has provided in monster detail.');
		}
		
		$this->view->monster = $rowMonster;
		
		// ドロップ装備およびアイテム ID を取得
		$monsterDrop = new Model_TrnMonsterDrop();
		$rowsMonsterDrop = $monsterDrop->getDrop($rowMonster->wid);

		$drop = array();
		foreach ($rowsMonsterDrop as $rowMonsterDrop) {
			$drop[$rowMonsterDrop->equip_item_flg][] = $rowMonsterDrop->dropitem_wid;
		}
		
		// ドロップ装備データを取得
		$equip = new Model_TrnEquipment();
		$rowsEquip = $equip->getByWid($drop[1]);
		$dropEquip = array();
		foreach ($rowsEquip as $rowEquip) {
			$dropEquip[$rowEquip->id] = $rowEquip;
		}

		// ドロップアイテムデータを取得
		$item = new Model_TrnItem();
		$rowsItem = $item->getByWid($drop[2]);
		$dropItem = array();
		foreach ($rowsItem as $rowItem) {
			$dropItem[$rowItem->id] = $rowItem;
		}
		
		$this->view->dropEquip = $dropEquip;
		$this->view->dropItem = $dropItem;
		
		$equipmentGroup = new Model_MstEquipmentGroup();
		$this->view->equipDir = $equipmentGroup->fetchAllArray('wid', 'directory');
		
		$this->view->headTitle($rowMonster->name . ' < モンスター', 'PREPEND');
		
		// パンくず設定
        $topicPath = array();
        $topicPath[] = array('name' => 'モンスター', 'url' => '/monster/');
        $topicPath[] = array('name' => $rowMonster->name);
		$this->view->topicPath = $topicPath;
		
		// META 設定
		$this->view->headMeta()->appendName('keywords', $rowMonster->name);
		
		$description = 'Lv.' . $rowMonster->level
		             . ' / 経験値 : ' . $rowMonster->exp
		             . ' / HP : ' . $rowMonster->hp
					 . ' / MP : ' . $rowMonster->mp
					 . ' / ' . str_replace(PHP_EOL, '　', $rowMonster->description);
		$this->view->headMeta()->appendName('description', $description);
	}
}
